package com.it.boot.config;


import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateDeserializer;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalTimeDeserializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateSerializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalTimeSerializer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.format.FormatterRegistry;
import org.springframework.format.datetime.standard.DateTimeFormatterRegistrar;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.Date;

import static com.it.boot.config.Const.*;

@Configuration
public class SerializerConfig {

    /**
     * 配置URL参数中的日期时间转换
     */
    @Bean
    public WebMvcConfigurer dateTimeFormatterConfigurer() {
        return new WebMvcConfigurer() {
            @Override
            public void addFormatters(FormatterRegistry registry) {
                DateTimeFormatterRegistrar registrar = new DateTimeFormatterRegistrar();
                registrar.setDateFormatter(FMT_DATE);
                registrar.setTimeFormatter(FMT_TIME);
                registrar.setDateTimeFormatter(FMT_DATE_TIME);
                registrar.registerFormatters(registry);

                // 注册传统 Date 类型的转换器
                registry.addConverter(String.class, Date.class, source -> {
                    try {
                        return new SimpleDateFormat(Const.DATE_TIME_FORMAT).parse(source);
                    } catch (Exception e) {
                        throw new IllegalArgumentException("日期格式必须为: " + Const.DATE_TIME_FORMAT);
                    }
                });
            }
        };
    }

    /**
     * 配置body参数中的日期时间转换,处理使用json序列化, eg: 接收body中参数 或者返回值
     */
    @Primary // 关键注解
    @Bean
    public ObjectMapper objectMapper() {
        ObjectMapper objectMapper = new ObjectMapper();
        JavaTimeModule javaTimeModule = new JavaTimeModule();
        // 序列化配置
        javaTimeModule.addSerializer(LocalDateTime.class, new LocalDateTimeSerializer(FMT_DATE_TIME));
        javaTimeModule.addSerializer(LocalDate.class, new LocalDateSerializer(Const.FMT_DATE));
        javaTimeModule.addSerializer(LocalTime.class, new LocalTimeSerializer(Const.FMT_TIME));

        // 反序列化配置
        javaTimeModule.addDeserializer(LocalDateTime.class, new LocalDateTimeDeserializer(FMT_DATE_TIME));
        javaTimeModule.addDeserializer(LocalDate.class, new LocalDateDeserializer(FMT_DATE));
        javaTimeModule.addDeserializer(LocalTime.class, new LocalTimeDeserializer(FMT_TIME));

        objectMapper.registerModule(javaTimeModule);
        // 配置传统Date类型的序列化格式
        objectMapper.setDateFormat(new SimpleDateFormat(DATE_TIME_FORMAT));
        // 禁用日期转时间戳
        objectMapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
        // 禁用调整日期到上下文时区
        objectMapper.disable(DeserializationFeature.ADJUST_DATES_TO_CONTEXT_TIME_ZONE);
        // 其他可选配置
        objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        return objectMapper;
    }
}